//
//  HSYCustomArcLayer+Bezier.m
//  CustomNavigationController
//
//  Created by key on 2017/2/22.
//  Copyright © 2017年 huangsongyao. All rights reserved.
//

#import "HSYCustomArcLayer+Bezier.h"

#define DEGREES_TO_RADIANS(angle)       ((angle) / 180.0 * M_PI)            //角度转弧度

@implementation HSYCustomArcLayer (Bezier)

- (UIBezierPath *)drawBezierPathByArcRadius:(CGFloat)radius arcCenter:(CGPoint)center startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:clockwise];
    return bezierPath;
}

- (void)drawBezierPathByProgress:(CGFloat)progress lineWidth:(CGFloat)width strokeColor:(UIColor *)color inContext:(CGContextRef)ctx
{
    CGFloat radius = self.bounds.size.width;
    CGFloat angle = 360 * fabs(progress);
    CGFloat endAngle = DEGREES_TO_RADIANS(angle) - DEGREES_TO_RADIANS(90);
    UIBezierPath *bezierPath = [self drawBezierPathByArcRadius:(radius - width)/2
                                                     arcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.width/2)
                                                    startAngle:-M_PI_2
                                                      endAngle:endAngle
                                                     clockwise:YES];
    CGContextSetRGBStrokeColor(ctx, color.red, color.green, color.blue, 1.0f);
    CGContextSetLineWidth(ctx, width);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextAddPath(ctx, bezierPath.CGPath);
    CGContextStrokePath(ctx);
}

@end
